home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_014 / termcap / testtcp.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  352 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  TEST PROGRAM
  22.  *
  23.  *    testtcp   test termcap functions
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    test routines
  28.  *    termcap test
  29.  *
  30.  *  SYNOPSIS
  31.  *
  32.  *    testtcp [-efns] terminal [capability [capability ...]]
  33.  *
  34.  *        -e  =>   expand string capability given by -s
  35.  *        -f  =>   determine boolean capabilities for terminal
  36.  *        -n  =>   determine numeric capabilities for terminal
  37.  *        -s  =>   determine string capabilities for terminal
  38.  *
  39.  *        terminal =>  terminal name as given in termcap file
  40.  *        capability => a boolean, numeric, or string capability
  41.  *
  42.  *        NOTE:  All capabilities must be of same type, as
  43.  *               given by [-fns].
  44.  *
  45.  *        If terminal is only argument then entire entry is
  46.  *        printed.
  47.  *
  48.  *  DESCRIPTION
  49.  *
  50.  *    Provides way to test termcap functions.  Can find
  51.  *    and print an entire termcap terminal entry, or various
  52.  *    capabilities from the entry.
  53.  *
  54.  *  AUTHOR
  55.  *
  56.  *    Fred Fish
  57.  *
  58.  */
  59.  
  60. #include <stdio.h>
  61.  
  62. #define TRUE 1
  63. #define FALSE 0
  64. #define NO_FILE     -1            /* Returned if can't open file */
  65. #define NO_ENTRY  0            /* Returned if can't find entry */
  66. #define SUCCESS   1            /* Returned if entry found ok */
  67. #define TRUNCATED 2            /* Returned if entry found but trunc */
  68. #define BUFFER_SIZE 1024
  69.  
  70. int eflag = FALSE;
  71. int fflag = FALSE;
  72. int nflag = FALSE;
  73. int sflag = FALSE;
  74.  
  75. int got_terminal = FALSE;
  76. int got_capability = FALSE;
  77.  
  78. short ospeed = 0;
  79. char PC = '\000';
  80.  
  81.  
  82. /*
  83.  *  FUNCTION
  84.  *
  85.  *    main   termcap test entry point
  86.  *
  87.  *  KEY WORDS
  88.  *
  89.  *    main
  90.  *
  91.  *  SYNOPSIS
  92.  *
  93.  *    main(argc,argv)
  94.  *    int argc;
  95.  *    char *argv[];
  96.  *
  97.  *  DESCRIPTION
  98.  *
  99.  *    This is where the termcap test starts executing.  All argument list
  100.  *    switches are processed first, then all the specified
  101.  *    capability identification strings are processed.
  102.  *
  103.  */
  104.  
  105. /*
  106.  *  PSEUDO CODE
  107.  *
  108.  *    Begin main
  109.  *        Process command line options.
  110.  *        For each argument list field
  111.  *        If field was not erased during option processing
  112.  *            If terminal name field not yet processed then
  113.  *            Process an assumed terminal name field.
  114.  *            Set terminal name processed flag.
  115.  *            Else
  116.  *            Process a capability field.
  117.  *            Set capability field processed flag.
  118.  *            End if
  119.  *        End if
  120.  *        End for
  121.  *        If no capabilities processed then
  122.  *        Simply dump buffer.
  123.  *        End if
  124.  *    End main
  125.  *
  126.  */
  127.  
  128. main(argc, argv)
  129. int argc;
  130. char *argv[];
  131. {
  132.     char *argp;
  133.     int argnum;
  134.     char buffer[BUFFER_SIZE];
  135.  
  136.     options(argc,argv);
  137.     for (argnum = 1; argnum < argc; argnum++) {
  138.         if ((argp = argv[argnum]) != NULL) {
  139.         if (!got_terminal) {
  140.         terminal(buffer,argp);
  141.         got_terminal = TRUE;
  142.         } else {
  143.         capability(argp);
  144.         got_capability = TRUE;
  145.         }
  146.         }
  147.     }
  148.     if (got_terminal && !got_capability) {
  149.     printf("%s",buffer);
  150.     }
  151. }
  152.  
  153. /*
  154.  *  FUNCTION
  155.  *
  156.  *    options   process command line options
  157.  *
  158.  *  SYNOPSIS
  159.  *
  160.  *    options(argc,argv)
  161.  *    int argc;
  162.  *    char *argv[];
  163.  *
  164.  *  DESCRIPTION
  165.  *
  166.  *    Scans argument list, processing each switch as it is
  167.  *    found.  The pointer to each switch string is then
  168.  *    replaced with a NULL to effectively erase the switch
  169.  *    argument.
  170.  *
  171.  */
  172.  
  173. /*
  174.  *  PSEUDO CODE
  175.  *
  176.  *    Begin options
  177.  *        For each argument in the argument list
  178.  *        Get pointer to first char of argument.
  179.  *        If the argument is a switch then
  180.  *            Replace argument pointer with NULL.
  181.  *            Look at next argument character.
  182.  *            While there is another argument character
  183.  *            Switch on the argument character
  184.  *            Case "EXPAND":
  185.  *                Set expand (e) flag.
  186.  *                Break out of switch.
  187.  *            Case "BOOLEAN":
  188.  *                Set boolean (f) flag.
  189.  *                Break out of switch.
  190.  *            Case "NUMERIC":
  191.  *                Set numeric flag.
  192.  *                Break out of switch.
  193.  *            Case "STRING":
  194.  *                Set string flag.
  195.  *                Break out of switch.
  196.  *            Default:
  197.  *                Abort with usage message.
  198.  *            End switch
  199.  *            End while
  200.  *        End if
  201.  *        End for
  202.  *    End options
  203.  *
  204.  */
  205.  
  206. options(argc, argv)
  207. int argc;
  208. char *argv[];
  209. {
  210.     int i;
  211.     char c;        /* 1st char of current command-line argument */
  212.     char *cp;        /* current argument pointer */
  213.  
  214.     for (i=1; i<argc; i++) {
  215.         cp = argv[i];
  216.         if (*cp == '-') {
  217.             argv[i] = NULL;
  218.         cp++;
  219.         while (c = *cp++) {
  220.             switch (c) {
  221.         case 'e':
  222.             eflag = TRUE;
  223.             break;
  224.         case 'f':
  225.             fflag = TRUE;
  226.                 break;
  227.             case 'n':
  228.             nflag = TRUE;
  229.                 break;
  230.             case 's':
  231.             sflag = TRUE;
  232.                 break;
  233.             default:
  234.                 usage();
  235.             }
  236.             }
  237.         }
  238.     }
  239. }
  240.  
  241. /*
  242.  *  FUNCTION
  243.  *
  244.  *    usage   give usage message and abort
  245.  *
  246.  *  KEY WORDS
  247.  *
  248.  *    usage
  249.  *    help processing
  250.  *    abort locations
  251.  *
  252.  *  SYNOPSIS
  253.  *
  254.  *    usage()
  255.  *
  256.  *  DESCRIPTION
  257.  *
  258.  *    Usage is typically called when a problem has been
  259.  *    detected in the argument list.
  260.  *    It prints a usage message and exits.
  261.  *
  262.  */
  263.  
  264. /*
  265.  *  PSEUDO CODE
  266.  *
  267.  *    Begin usage
  268.  *        Print usage message.
  269.  *        Exit.
  270.  *    End usage
  271.  *
  272.  */
  273.  
  274. usage()
  275. {
  276.     printf("Usage: testtcp [-efns] terminal [capability [capability ... ]]\n");
  277.     exit();
  278. }
  279.  
  280.  
  281. terminal(buffer,name)
  282. char *buffer;
  283. char *name;
  284. {
  285.     int status;
  286.  
  287.     status = tgetent(buffer,name);
  288.     switch (status) {
  289.     case NO_FILE:
  290.     fprintf(stderr,"Can't find a termcap data base file.\n");
  291.     exit();
  292.     case NO_ENTRY:
  293.     fprintf(stderr,"Can't find entry \"%s\"\n",name);
  294.     exit();
  295.     case TRUNCATED:
  296.     fprintf(stderr,"Warning --- entry \"%s\" too long\n",name);
  297.     break;
  298.     case SUCCESS:
  299.         break;
  300.     default:
  301.         fprintf(stderr,"? tgetent returned illegal status %d\n",status);
  302.     exit();
  303.     }
  304. }
  305.  
  306. capability(id)
  307. char *id;
  308. {
  309.     int value;
  310.     char buffer[256];
  311.     char *area;
  312.     char *ep, *tgoto();
  313.  
  314.     if (fflag) {
  315.     value = tgetflag(id);
  316.     if (value) {
  317.         printf("%s TRUE\n",id);
  318.     } else {
  319.         printf("%s FALSE\n",id);
  320.     }
  321.     } else if (nflag) {
  322.     value = tgetnum(id);
  323.     printf("%s = %o octal %d decimal\n",id,value,value);
  324.     } else if (sflag) {
  325.     area = buffer;
  326.     tgetstr(id,&area);
  327.     if (eflag) {
  328.         ep = tgoto(buffer,75,23);
  329.     }
  330.     doprint(id,buffer);
  331.     if (eflag) {
  332.         doprint(id,ep);
  333.     }
  334.     }
  335. }
  336.  
  337. doprint(id,cp)
  338. char *id;
  339. char *cp;
  340. {
  341.     printf("%s = ",id);
  342.     for ( ; *cp != NULL; cp++) {
  343.     if (*cp < 040) {
  344.         printf("^%c ",*cp |= 0100);
  345.     } else {
  346.         printf("%c ",*cp);
  347.     }
  348.     }
  349.     printf("\n");
  350. }
  351.  
  352.